#include "getBrightnessThread.h"
#include <QDBusInterface>
#include <QDBusReply>
#include <QDebug>

GetBrightnessThread::GetBrightnessThread(QString edidHash, QString i2cBus)
{
    this->edidHash = edidHash;
    this->i2cBus   = i2cBus;
    this->exitFlag = false;
}

GetBrightnessThread::~GetBrightnessThread()
{

}

void GetBrightnessThread::run()
{
    int times = 600;
    QDBusInterface ukccIfc("com.control.center.qt.systemdbus",
                           "/",
                           "com.control.center.interface",
                           QDBusConnection::systemBus());
    QDBusReply<int> reply;
    while (times) {
        //防止调用terminate不会结束线程，锁屏在龙芯架构上出现过
        if (exitFlag) {
            return;
        }
        if (times %10 != 0) {
            times--;
            msleep(100);
            continue;
        }
        if (this->edidHash == "") {
            Q_EMIT getBrightnessFinished(-1);
            return;
        }
        reply = ukccIfc.call("getDisplayBrightness", this->edidHash, this->i2cBus);
        if (reply.isValid() && reply.value() >= 0 && reply.value() <= 100) {
           Q_EMIT getBrightnessFinished(reply.value());
           return;
        }
        times--;
        msleep(100);
    }
    Q_EMIT getBrightnessFinished(-1);
    return;
}

void GetBrightnessThread::setExit(bool b)
{
    exitFlag = b;
}


